package com.fit.pages.registration;

import java.util.Date;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.client.ClientDAO;
import com.fit.dao.company.CompanyDAO;
import com.fit.dao.registration.LicencePlateDAO;
import com.fit.dao.registration.RegistrationDAO;
import com.fit.dao.vehicle.VehicleDAO;
import com.fit.entities.Client;
import com.fit.entities.LicencePlate;
import com.fit.entities.Registration;
import com.fit.entities.Vehicle;
import com.fit.model.type.VehicleType;

@UserAccess
@AdminAccess
public class InsertRegistration {

	@Property
	@Persist
	private Vehicle vehicle;

	@Inject
	private VehicleDAO vehicleDAO;

	@Property
	@SessionState(create = false)
	private LicencePlate licencePlate;

	@Inject
	private LicencePlateDAO licencePlateDAO;
	
	@Property
	@SessionState(create = false)
	private Registration registration;

	@Inject
	private RegistrationDAO registrationDAO;

	@Inject
	private Messages messages;

	@Inject
	private CompanyDAO companyDAO;

	@Property
	@Persist
	private String chassisNumber;

	@Property
	@Persist
	private String personalId;

	@Persist
	@Property
	private boolean second;

	@Persist
	@Property
	private boolean third;

	@Persist
	@Property
	private boolean showVehicle;

	@Persist
	@Property
	private boolean showClient;

	@Persist
	@Property
	private String error;

	@Persist
	@Property
	private String confirmation;

	@Property
	@Persist
	private VehicleType vehicleType;

	@Property
	@Persist
	private Client client;

	@Inject
	private ClientDAO clientDAO;

	@Persist
	private boolean updateVehicle;

	@Persist
	private boolean updateClient;

	public SelectModel getTypes() {
		return new EnumSelectModel(VehicleType.class, messages);
	}

	void onPrepare() {
		if (vehicle == null) {
			vehicle = new Vehicle();
		}
		if (client == null) {
			client = new Client();
		}
		licencePlate = new LicencePlate();
		registration = new Registration();
	}

	Object onSubmitFromFindForm() {
		second = false;
		vehicle = vehicleDAO.findByChassisNumber(chassisNumber);
		showVehicle = true;
		if (vehicle == null) {
			vehicle = new Vehicle();
			vehicle.setChassisNumber(chassisNumber);
			updateVehicle = false;
			return null;
		}
		updateVehicle = true;
		return null;
	}

	Object onSubmitFromFindForm1() {
		third = false;
		client = clientDAO.findByPersonalId(personalId);
		showClient = true;
		if (client == null) {
			client = new Client();
			client.setPersonalId(personalId);
			updateClient = false;
			return null;
		}
		updateClient = true;
		return null;
	}

	Object onSubmitFromCreateForm() {
		if (updateVehicle) {
			vehicleDAO.update(vehicle);
			confirmation = "Vehicle successfully updated!";
			second = true;
			third = false;
			return null;
		}
		Vehicle temp = vehicleDAO.findByChassisNumber(vehicle
				.getChassisNumber());

		if (temp == null) {
			if (vehicleType == null) {
				error = "You didn't choose vehicle type!";
				return null;
			}
			vehicle.setTypeOfVehicle(vehicleType.toString());
			if (vehicleDAO.save(vehicle)) {
				confirmation = "Vehicle successfully inserted!";
				second = true;
				third = false;
				return null;
			}
			confirmation = "Vehicle is null!";
			return null;
		}

		error = "Vehicle with chassis number " + vehicle.getChassisNumber()
				+ " already exists!";

		return null;
	}

	Object onSubmitFromCreateForm1() {
		if (updateClient) {
			clientDAO.update(client);
			confirmation = "Client successfully updated!";
			third = true;
			second = false;
			return null;
		}
		error = "";
		confirmation = "";
		Client temp = clientDAO.findByPersonalId(client.getPersonalId());

		if (temp == null) {
			client.setPersonalId(personalId);
			if (clientDAO.save(client)) {
				confirmation = "Client successfully inserted!";
				third = true;
				second = false;
				return null;
			}
			error = "Client is null!";
			return null;
		}

		error = "Client with " + client.getId() + " ID already exists!";

		return null;
	}

	Object onSubmitFromCreateForm2() {
		error = "";
		confirmation = "";
		LicencePlate temp = licencePlateDAO.findByLicencePlateNo(licencePlate
				.getLicencePlateNo());

		if (temp == null) {
			licencePlate.setDate(new Date());
			licencePlate.setClient(client);
			licencePlate.setVehicle(vehicle);
			if (licencePlateDAO.save(licencePlate)) {
				registration.setLicencePlate(licencePlate);
				registration.setActive(false);
				registration.setDate(licencePlate.getDate());
				Date expiryDate = new Date();
				expiryDate.setYear(licencePlate.getDate().getYear() + 1);
				registration.setExpiryDate(expiryDate);
				registration.setCompany(companyDAO.findById(new Long(Integer
						.toString(3))));
				registrationDAO.save(registration);
				confirmation = "Registration successfully inserted!";
				vehicle = null;
				client = null;
				chassisNumber = "";
				personalId = "";
				second = false;
				third = false;
				showClient = false;
				showVehicle = false;
				return null;
			}
			confirmation = "Client is null!";
			return null;
		}
		error = "Licence plate " + licencePlate.getLicencePlateNo()
				+ " already exists!";

		return null;
	}

	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation = "";
		return null;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error = "";
		return null;
	}

	public boolean isErrorNotNull() {
		if (error == null || error == "") {
			return false;
		}
		return true;
	}

	public boolean isConfirmationNotNull() {
		if (confirmation == null || confirmation == "") {
			return false;
		}
		return true;
	}

}
